/**
  ******************************************************************************
  * @file           : mp3_decode_stream.h
  * @version        : v0.1
  * @brief          : Header for mp3_decode_stream.c file.
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __MP3_DECODE_STREAM__H__
#define __MP3_DECODE_STREAM__H__

#ifdef __cplusplus
 extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include <stdbool.h>
#include <string.h>
#include "mp3dec.h"
#include "mp3_def.h"

/* Exported macro ------------------------------------------------------------*/
#define RAW_DATA_SIZE               (512 * 4)

/* Exported types ------------------------------------------------------------*/


typedef enum
{
    RAWBUF_READY = 0,
    RAWBUF_READY_EOF,
    RAWBUF_NOSYNC,
    RAWBUF_SYNC,
    RAWBUF_EOF
} RawBuf_State_t;

typedef struct RawBuf_t
{
    _Alignas(4) volatile char buf[RAW_DATA_SIZE];
    int ptr;
    int remain;
    bool is_fread_end;
    RawBuf_State_t state;
    int (*f_read)(void *buf, unsigned int *length); /* 0 = OK, -1 = END */
} RawBuf_t;

#define STATUS_X2BUFF_0_REQUEST    (0x0001UL)
#define STATUS_X2BUFF_1_REQUEST    (0x0002UL)
#define STATUS_PCMBUFF_EXIST       (0x0004UL)

/**
  * @brief  member [status]:
  * X2Buff_0_Request [0:0]
  * X2Buff_1_Request [1:1]
  * PCMBuff_Empty    [2:2]
  */
typedef struct mp3_decode_stream_t
{
    HMP3Decoder decoder;
    MP3FrameInfo frameinfo;
    MP3FrameInfo nextFrameinfo;
    RawBuf_t raw;
    volatile uint8_t status;
    short x2buff[LAY3_FRAME_SAMPLE_NUM * 2 * 2];
    uint32_t x2SampleBuff[2];
    short pcm[LAY3_FRAME_SAMPLE_NUM * 2];
} mp3_decode_stream_t;

extern mp3_decode_stream_t *mp3_decode_stream;


/* Exported constants --------------------------------------------------------*/


/* Exported macro ------------------------------------------------------------*/


/* Exported functions prototypes ---------------------------------------------*/
void dma_dac_cplt_irq(DMA_HandleTypeDef *hdma);
void dma_dac_half_irq(DMA_HandleTypeDef *hdma);
void row_buff_align_cplt_irq(DMA_HandleTypeDef *hdma);

int mp3_decode_stream_init();
void mp3_set_read_fun(int (*f_read)(void *buf, unsigned int *length));
void mp3_decode_stream_start(void);
/* Private defines -----------------------------------------------------------*/


#ifdef __cplusplus
}
#endif

#endif /* __MP3_DECODE_STREAM__H__ */
